package com.webmeeting.jsfbeans.chat.wordchat;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.myframework.utils.logutil.WMLogger;
import com.myframework.utils.stringutil.StringUtil;
import com.webmeeting.chat.wordchat.bl.ChatRoom;
import com.webmeeting.chat.wordchat.dao.entities.User;
import com.webmeeting.chat.wordchat.servlets.WordChatInitServlet;

@ManagedBean(name = "createRoomBean_wordchat")
@RequestScoped
public class CreateRoomBean {

	// Header message
	private String	headerMsg;
	// Room name
	private String	roomName;
	// Access password
	private String	pwForAccess;
	// Access password repeat
	private String	pwForAccessRe;
	// Delete password
	private String	pwForDelete;
	// Delete password repeat
	private String	pwForDeleteRe;

	public String createRoom(String contextPath) {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
		User currUser = (User) session.getAttribute("user");

		if (StringUtil.isEmpty(this.roomName)) {
			this.headerMsg = "Room name can not be empty";
			return "";
		} else if (!StringUtil.isEmpty(this.pwForAccess) && !this.pwForAccess.equals(this.pwForAccessRe)) {
			this.headerMsg = "Access password should be same";
			return "";
		} else if (!StringUtil.isEmpty(this.pwForDelete) && !this.pwForDelete.equals(this.pwForDeleteRe)) {
			this.headerMsg = "Delete password should be same";
			return "";
		} else if (isRoomNameDuplicate(this.roomName)) {
			this.headerMsg = "Room name has already exist, pleas enter another one";
			return "";
		} else {
			ChatRoom room = new ChatRoom();
			room.setRoomName(this.roomName);
			room.setPwForAccess(this.pwForAccess);
			room.setPwForDelete(this.pwForDelete);
			WordChatInitServlet.addOnlineRoom(room);
			WMLogger.debug("Create a new room: " + this.roomName + " by " + currUser.getUsername());
			return contextPath + "/chat/wordchat/roomlist.xhtml";
		}
	}

	private boolean isRoomNameDuplicate(String roomName) {
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();
		for (ChatRoom room : rooms) {
			if (roomName.equals(room.getRoomName())) {
				return true;
			}
		}
		return false;
	}

	public String getHeaderMsg() {
		return headerMsg;
	}

	public void setHeaderMsg(String headerMsg) {
		this.headerMsg = headerMsg;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public String getPwForAccess() {
		return pwForAccess;
	}

	public void setPwForAccess(String pwForAccess) {
		this.pwForAccess = pwForAccess;
	}

	public String getPwForAccessRe() {
		return pwForAccessRe;
	}

	public void setPwForAccessRe(String pwForAccessRe) {
		this.pwForAccessRe = pwForAccessRe;
	}

	public String getPwForDelete() {
		return pwForDelete;
	}

	public void setPwForDelete(String pwForDelete) {
		this.pwForDelete = pwForDelete;
	}

	public String getPwForDeleteRe() {
		return pwForDeleteRe;
	}

	public void setPwForDeleteRe(String pwForDeleteRe) {
		this.pwForDeleteRe = pwForDeleteRe;
	}

}
